const { imgUrl } = getApp().globalData;
import { publicApi } from "@/utils/request/api/public";
import { mineApi } from "@/utils/request/api/mine";
Page({
  data: {
    type: 'add',
    imgUrl: imgUrl + "mine/",
    showPre: false,
    showImageUrl: '',
    add: {
      name: "", //姓名
      position: "", //职位
      certifications: [],
    },
  },
  showMore(event) {
    let index = event.currentTarget.dataset.index;
    let add = JSON.parse(JSON.stringify(this.data.add));
    add.certifications.forEach(item => {
      item.isMore = false
    })
    add.certifications[index].isMore = !add.certifications[index].isMore
    this.setData({
      add
    })
  },
  hideMore() {
    let add = JSON.parse(JSON.stringify(this.data.add));
    add.certifications.forEach(item => {
      item.isMore = false
    })
    this.setData({
      add
    })
  },
  onClose() {
    this.setData({
      showPre: false,
    })
  },
  change(event) {
    const {
      currentTarget: {
        dataset: { type },
      },
    } = event;
    let add = JSON.parse(JSON.stringify(this.data.add));
    add[type] = event.detail.value;
    this.setData({
      add,
    });
  },
  chooseFile(event) {
    const that = this;
    wx.chooseMessageFile({
      count: 1,
      type: "file",
      extension: ["pdf", "docx", "jpg", "png", "jpeg"],
      success(res) {
        const filePath = res.tempFiles[0].path;
        const formData = {
          fileVal: filePath,
          name: res.tempFiles[0].name,
        };

        publicApi
          .uploadFile({
            filePath,
            formData,
          })
          .then((res2) => {
            let obj = JSON.parse(JSON.stringify(that.data.add));
            obj.certifications.push({
              name: res.tempFiles[0].name, //项目名称
              issuing_authority: "建筑协会", //发证机关
              issuing_date: "2026-05-21", //发证时间
              is_expire: false, //是否有到期时间
              expire_date: "2030-05-21", //到期时间
              file_url: res2.data, //证书文件
              isMore: false
            });
            that.setData({
              add: obj,
            });
          });
      },
    });
  },
  check() {
    let add = JSON.parse(JSON.stringify(this.data.add));
    if (!add.name) {
      wx.showToast({
        title: "姓名不能为空!",
        icon: "error",
        duration: 2000,
      })
      return false;
    }
    if (!add.position) {
      wx.showToast({
        title: "职务不能为空!",
        icon: "error",
        duration: 2000,
      })
      return false;
    }
    if (!add.certifications.length === 0) {
      wx.showToast({
        title: "请上传证书!",
        icon: "error",
        duration: 2000,
      })
      return false;
    }
    return true;
  },
  confirmAdd() {
    if (!this.check()) {
      return;
    }
    let add = JSON.parse(JSON.stringify(this.data.add));
    add.company_id = wx.getStorageSync('company_id');
    add.id = 0;
    mineApi.addCompanyUser(add).then(res => {
      wx.showToast({
        title: "添加成功",
        icon: "success",
        duration: 2000,
        success: function () {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            });
          }, 2000);
        }
      })
    })
  },
  deleteFile(event) {
    let index = event.currentTarget.dataset.index;
    let add = JSON.parse(JSON.stringify(this.data.add));
    add.certifications.splice(index, 1);
    this.setData({
      add,
    })
  },
  handlePreImg(event) {
    let index = event.currentTarget.dataset.index;
    this.setData({
      showPre: true,
      showImageUrl: this.data.add.certifications[index].file_url
    })
  },
  confirmEdit() {
    if (!this.check()) {
      return;
    }
    let add = JSON.parse(JSON.stringify(this.data.add));
    mineApi.addCompanyUser(add).then(res => {
      wx.showToast({
        title: "修改成功",
        icon: "success",
        duration: 2000,
        success: function () {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            });
          }, 2000);
        }
      })
    })
  },
  afterRead() {},
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('edit', data => {
      console.log('data === ', data)
      this.setData({
        add: data.data,
        type: 'edit'
      }, () => {
        wx.setNavigationBarTitle({
          title: data.data.name
        });
      })
    });
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onShareAppMessage() {
    return {
      title: "",
    };
  },
});
